import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

import { useAuthStore } from '@/store'

import Layout from '../layout/index.vue'
import { useResourceStore } from '@/store/resource'

//import _route from './menu'
import { useI18nStore } from '@/store/i18n'


const router = createRouter({
    history: createWebHashHistory(),
    routes : [
        {
            path : '/login',
            name : 'login',
            meta : {title : 'login', noAuth : true},
            component : () => import('../pages/login.vue')
        },

        {
            path : '/:pathMatch(.*)*',
            component : () => import('../pages/404.vue')
        },

        {
            path : '/setting',
            meta : {title : 'setting'},
            component : Layout,
            children : [
                {
                    path : '',
                    name : 'setting',
                    component : () => import('../pages/setting.vue')
                }
            ]
        }

        
        //..._route
        
    ],
})

// if(import.meta.env.B_USE_ROUTER_MENU == '1') {
//     _route.forEach(route => {
//         router.addRoute(route)
//     })
// }


export const asyncPageComponents:any = {
'system/dictionary/index.vue' : () => import('@/pages/system/dictionary/index.vue'),
'system/role/index.vue' : () => import('@/pages/system/role/index.vue'),
'system/member/index.vue' : () => import('@/pages/system/member/index.vue'),
'system/language/index.vue' : () => import('@/pages/system/language/index.vue'),
'system/language/locale.vue' : () => import('@/pages/system/language/locale.vue'),
'system/language/lang.vue' : () => import('@/pages/system/language/lang.vue'),
'system/menu/index.vue' : () => import('@/pages/system/menu/index.vue'),
'config/publicChain/index.vue' : () => import('@/pages/config/publicChain/index.vue'),
'config/token/index.vue' : () => import('@/pages/config/token/index.vue'),
'config/tool/index.vue' : () => import('@/pages/config/tool/index.vue'),
'dapp/category/index.vue' : () => import('@/pages/dapp/category/index.vue'),
'dapp/index.vue' : () => import('@/pages/dapp/index.vue'),
'appVersion/index.vue' : () => import('@/pages/appVersion/index.vue'),
'activity/earncoin/index.vue' : () => import('@/pages/activity/earncoin/index.vue'),
'article/help/index.vue' : () => import('@/pages/article/help/index.vue'),
'article/help/category/index.vue' : () => import('@/pages/article/help/category/index.vue'),
'article/help/article/index.vue' : () => import('@/pages/article/help/article/index.vue'),
'article/notice/index.vue' : () => import('@/pages/article/notice/index.vue'),
'banner/index.vue' : () => import('@/pages/banner/index.vue'),
}

let initPermission = false
function getDefaultPath(routes:RouteRecordRaw[], path:string) {
    for(let i = 0; i < routes.length; i ++) {
        const route = routes[i]
        if(route.name != 'login' && route.name != 'loginPlatform') {
            path = route.path
            if(!route.children || route.children.length == 0) {
                break
            }else {
                path = getDefaultPath(route.children, path)
            }
        }
    }

    return path
}

router.beforeEach((to, from, next) => {
    const i18n = useI18nStore()
    if(to.meta && to.meta.title) {
        document.title = i18n.t(to.meta.title as string)
    }

    let authStore = useAuthStore()


    if(!to.meta.noAuth) { //需要登录
        if(!authStore.auth) {

            next('/login')
            return
        }
        
    }

    
    
    if(!to.meta.noAuth && !authStore.auth) {
        next({path : '/login', query : {redirect : encodeURIComponent(to.fullPath)}})
        return
    }


    if((!to.name || authStore.auth) && !initPermission) {
        initPermission = true
        const resource = useResourceStore()
        resource.getResource().then(() => {
            // if(router.hasRoute(to.path)) {
            //     next({path : to.path})
            // }else {
            //     next({path : getDefaultPath(router.getRoutes(), '/')})
            // }

            next({path : to.path})

        })

        return
    }

    if(to.redirectedFrom) {
        if(to.redirectedFrom.path == to.path && to.redirectedFrom.fullPath != to.fullPath) {
            to.fullPath = to.redirectedFrom.fullPath
            to.query = {...to.redirectedFrom.query}
        }
    }

    next()
})

export default router